package com.binhai.springboot.org.service;

import com.binhai.springboot.org.bean.OrgBean;
import com.binhai.springboot.org.dao.OrgBeanDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class OrgService {

    @Autowired
    private OrgBeanDao beanDao;

    public List getAllData() {
        OrgBean orgBean = beanDao.selectOrgBeanById(2);
        return beanDao.getAllData();
    }
    private String  getOrgidsForString(OrgBean bean){
        String msg = "";
        msg=msg+bean.getId() + ",";
        if(bean.getChildren().size()>0){
            for (int i = 0; i < bean.getChildren().size(); i++) {
                msg=msg+getOrgidsForString( bean.getChildren().get(i));
            }
        }
        //
        return msg;
    }

        public List getAllTreeData() {

        return beanDao.getAllTreeData ();
    }

    public List selectData(String name) {

        return beanDao.selectData(name);

    }

    public Map saveData(OrgBean bean) {
        Map re = new HashMap ( );
        try {
            beanDao.insertData(bean);
            re.put ("result", "success");
        } catch (Exception e) {
            re.put ("result", "error");
            e.printStackTrace ( );
        }
        return re;
    }



    public Map updateData(OrgBean bean) {
        Map re = new HashMap();
        try {
            //修改数据
            beanDao.updateByPrimaryKeySelective(bean);

            re.put("result", "success");
        } catch (Exception e) {
            re.put("result", "error");
            e.printStackTrace();
        }
        return re;
    }

    public Map deleteData(OrgBean bean) {
        Map re = new HashMap();
        try {

            beanDao.deleteData(bean);
            re.put("result", "success");
        } catch (Exception e) {
            re.put("result", "error");
            e.printStackTrace();
        }
        return re;
    }
}
